package psn.hui.bus.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import psn.hui.bus.domain.Car;
import psn.hui.bus.domain.Check;
import psn.hui.bus.domain.Customer;
import psn.hui.bus.domain.Rent;
import psn.hui.bus.mapper.CarMapper;
import psn.hui.bus.mapper.CheckMapper;
import psn.hui.bus.mapper.CustomerMapper;
import psn.hui.bus.mapper.RentMapper;
import psn.hui.bus.service.CheckService;
import psn.hui.bus.vo.CheckVo;
import psn.hui.sys.constant.SysConstant;
import psn.hui.sys.domain.User;
import psn.hui.sys.utils.DataGridView;
import psn.hui.sys.utils.RandomUtil;
import psn.hui.sys.utils.WebUtils;
/**
 * 检查单管理服务接口实现类
 * @author hui
 *
 */
@Service
public class CheckServiceImpl implements CheckService{
	@Autowired
	private CheckMapper checkMapper;
	@Autowired
	private CarMapper carMapper;
	@Autowired
	private CustomerMapper customerMapper;
	@Autowired
	private RentMapper rentMapper;
	/**
	 * 添加检查单
	 * @param check
	 * @return
	 */
	public int addCheck(Check check) {
		int rs = checkMapper.insertSelective(check);
		//更改出租单的状态
		Rent rent = rentMapper.selectByPrimaryKey(check.getRentid());
		rent.setRentflag(SysConstant.RENT_FLAG_RETURN);
		rentMapper.updateByPrimaryKeySelective(rent);
		//改变车辆状态
		Car car = new Car();
		car.setCarnumber(rent.getCarnumber());
		car.setIsrenting(SysConstant.CAR_ISRENTING_FALSE);
		rs += carMapper.updateByPrimaryKeySelective(car);
		return rs;
	}
	
	/**
	 * 	根据出租单号，初始化检查单信息，获取车辆，出租单，客户信息
	 * @param rentid
	 * @return
	 */
	public Map<String, Object> initCheckFormData(String rentid){
		Map<String,Object> map = new HashMap<String, Object>();
		//1. 初始化检查单信息
		Check check = new Check();
		//设置检查单
		check.setCheckid(RandomUtil.createRandomStringUseTime(SysConstant.CHECKID_SUFFIX));
		//设置出租单号
		check.setRentid(rentid);
		//设置检查日期
		check.setCheckdate(new Date());
		//设置操作员
		User user = (User)WebUtils.getHttpSession().getAttribute("user");
		check.setOpername(user.getRealname());
		map.put("check", check);
		//2.获取出租单信息
		Rent rent = rentMapper.selectByPrimaryKey(rentid);
		map.put("rent", rent);
		//3.获取车辆信息
		Car car = carMapper.selectByPrimaryKey(rent.getCarnumber());
		map.put("car", car);
		//4.获取客户信息
		Customer customer = customerMapper.selectByPrimaryKey(rent.getIdentity());
		map.put("customer", customer);
		return map;
	}
	/**
	 * 	模糊查询检查单信息
	 * @param checkVo
	 * @return
	 */
	public DataGridView queryAllCheck(CheckVo checkVo) {
		Page<Object> page = PageHelper.startPage(checkVo.getPage(), checkVo.getLimit());
		List<Check> allCheck = checkMapper.queryAllCheck(checkVo);
		return new DataGridView(page.getTotal(), allCheck);
	}
	/**
	 * 	修改检查单信息
	 * @param check
	 * @return
	 */
	public int updateCheck(Check check) {
		return checkMapper.updateByPrimaryKeySelective(check);
	}
}
